package com.baohua.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.baohua.dao.BaseDao;
import com.baohua.dao.MemberDao;
import com.baohua.model.Member;

@Repository("memberDao")
public class MemberDaoImpl extends BaseDao implements MemberDao {

	/**
	 * 根据SQL语句查询会员
	 * 
	 * */
	@SuppressWarnings("unchecked")
	@Override
	public List<Member> queryMember(String sql, int page, int pageSize) {
		Query query = this.getSession().createQuery(sql)
				.setFirstResult(page).setMaxResults(pageSize);
		List<Member> list = query.list();
		return list;
	}
	/**
	 * 根据会员编号查询会员
	 * 
	 * */
	@Override
	public Member queryMemberBymenberNo(String memberNo) {
		Member member = (Member) this.getSession().createQuery("from Member m where m.memberNo=?").setString(0, memberNo).uniqueResult();
		return member;
	}
	/**
	 * 根据会员名字查询会员
	 * 
	 * */
	@Override
	public Member queryMemberByMemberName(String memberName) {
		Member member = (Member) this.getSession().createQuery("from Member m where m.memberName=?").setString(0, memberName).uniqueResult();
		return member;
	}
	/**
	 * 新增会员信息
	 * 
	 * */
	@Override
	public void addMember(Member member) {
		this.getHibernateTemplate().save(member);		
	}
	/**
	 * 删除会员信息
	 * 
	 * */
	@Override
	public void delMember(Member member) {
		this.getHibernateTemplate().delete(member);
		
	}
	/**
	 * 更新会员信息
	 * 
	 * */
	@Override
	public void updateMember(Member member) {
		this.getHibernateTemplate().update(member);		
	}
	/**
	 * 统计会员总数
	 * 
	 * */
	@Override
	public long countMember(String sql) {
		long count = (Long)this.getSession().createQuery(sql).uniqueResult();
		return count;
	}
	/**
	 * 根据会员ID查询会员信息
	 * 
	 * */
	@Override
	public Member queryMemberByMemberId(int memberId) {
		Member member = (Member) this.getSession().createQuery("from Member m where m.memberId=?").setLong(0,memberId ).uniqueResult();
		return member;
	}

}
